from django.http import JsonResponse

def get_pagination_params(request, default_page_size=10, max_page_size=100):
    try:
        page = int(request.GET.get('page', 1))
    except (TypeError, ValueError):
        page = 1

    try:
        page_size = int(request.GET.get('page_size', default_page_size))
    except (TypeError, ValueError):
        page_size = default_page_size

    if page < 1:
        page = 1
    if page_size < 1:
        page_size = default_page_size
    if page_size > max_page_size:
        page_size = max_page_size

    return page, page_size


def paginate_queryset(queryset, page, page_size):
    total = queryset.count()
    start = (page - 1) * page_size
    end = start + page_size
    items = list(queryset[start:end])
    has_next = end < total
    has_prev = start > 0
    return items, {
        'page': page,
        'page_size': page_size,
        'total': total,
        'has_next': has_next,
        'has_prev': has_prev,
        'total_pages': (total + page_size - 1) // page_size if page_size else 0,
    }


def json_list_response(items, pagination, serializer):
    return JsonResponse({
        'success': True,
        'data': [serializer(item) for item in items],
        'pagination': pagination
    }, status=200)